layout.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. 'use client'
  2. import type { FC, SVGProps } from 'react'
  3. import React, { useEffect, useMemo } from 'react'
  4. import { usePathname } from 'next/navigation'
  5. import useSWR from 'swr'
  6. import { useTranslation } from 'react-i18next'
  7. import { useBoolean } from 'ahooks'
  8. import {
  9. Cog8ToothIcon,
  10. // CommandLineIcon,
  11. Squares2X2Icon,
  12. // eslint-disable-next-line sort-imports
  13. PuzzlePieceIcon,
  14. DocumentTextIcon,
  15. PaperClipIcon,
  16. QuestionMarkCircleIcon,
  17. } from '@heroicons/react/24/outline'
  18. import {
  19. Cog8ToothIcon as Cog8ToothSolidIcon,
  20. // CommandLineIcon as CommandLineSolidIcon,
  21. DocumentTextIcon as DocumentTextSolidIcon,
  22. } from '@heroicons/react/24/solid'
  23. import Link from 'next/link'
  24. import s from './style.module.css'
  25. import classNames from '@/utils/classnames'
  26. import { fetchDatasetDetail, fetchDatasetRelatedApps } from '@/service/datasets'
  27. import type { RelatedApp, RelatedAppResponse } from '@/models/datasets'
  28. import AppSideBar from '@/app/components/app-sidebar'
  29. import Divider from '@/app/components/base/divider'
  30. import AppIcon from '@/app/components/base/app-icon'
  31. import Loading from '@/app/components/base/loading'
  32. import FloatPopoverContainer from '@/app/components/base/float-popover-container'
  33. import DatasetDetailContext from '@/context/dataset-detail'
  34. import { DataSourceType } from '@/models/datasets'
  35. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  36. import { LanguagesSupported } from '@/i18n/language'
  37. import { useStore } from '@/app/components/app/store'
  38. import { AiText, ChatBot, CuteRobot } from '@/app/components/base/icons/src/vender/solid/communication'
  39. import { Route } from '@/app/components/base/icons/src/vender/solid/mapsAndTravel'
  40. import { getLocaleOnClient } from '@/i18n'
  41. import { useAppContext } from '@/context/app-context'
  42. export type IAppDetailLayoutProps = {
  43. children: React.ReactNode
  44. params: { datasetId: string }
  45. }
  46. type ILikedItemProps = {
  47. type?: 'plugin' | 'app'
  48. appStatus?: boolean
  49. detail: RelatedApp
  50. isMobile: boolean
  51. }
  52. const LikedItem = ({
  53. type = 'app',
  54. detail,
  55. isMobile,
  56. }: ILikedItemProps) => {
  57. return (
  58. <Link className={classNames(s.itemWrapper, 'px-2', isMobile && 'justify-center')} href={`/app/${detail?.id}/overview`}>
  59. <div className={classNames(s.iconWrapper, 'mr-0')}>
  60. <AppIcon size='tiny' iconType={detail.icon_type} icon={detail.icon} background={detail.icon_background} imageUrl={detail.icon_url} />
  61. {type === 'app' && (
  62. <span className='absolute bottom-[-2px] right-[-2px] w-3.5 h-3.5 p-0.5 bg-white rounded border-[0.5px] border-[rgba(0,0,0,0.02)] shadow-sm'>
  63. {detail.mode === 'advanced-chat' && (
  64. <ChatBot className='w-2.5 h-2.5 text-[#1570EF]' />
  65. )}
  66. {detail.mode === 'agent-chat' && (
  67. <CuteRobot className='w-2.5 h-2.5 text-indigo-600' />
  68. )}
  69. {detail.mode === 'chat' && (
  70. <ChatBot className='w-2.5 h-2.5 text-[#1570EF]' />
  71. )}
  72. {detail.mode === 'completion' && (
  73. <AiText className='w-2.5 h-2.5 text-[#0E9384]' />
  74. )}
  75. {detail.mode === 'workflow' && (
  76. <Route className='w-2.5 h-2.5 text-[#f79009]' />
  77. )}
  78. </span>
  79. )}
  80. </div>
  81. {!isMobile && <div className={classNames(s.appInfo, 'ml-2')}>{detail?.name || '--'}</div>}
  82. </Link>
  83. )
  84. }
  85. const TargetIcon = ({ className }: SVGProps<SVGElement>) => {
  86. return <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" className={className ?? ''}>
  87. <g clipPath="url(#clip0_4610_6951)">
  88. <path d="M10.6666 5.33325V3.33325L12.6666 1.33325L13.3332 2.66659L14.6666 3.33325L12.6666 5.33325H10.6666ZM10.6666 5.33325L7.9999 7.99988M14.6666 7.99992C14.6666 11.6818 11.6818 14.6666 7.99992 14.6666C4.31802 14.6666 1.33325 11.6818 1.33325 7.99992C1.33325 4.31802 4.31802 1.33325 7.99992 1.33325M11.3333 7.99992C11.3333 9.84087 9.84087 11.3333 7.99992 11.3333C6.15897 11.3333 4.66659 9.84087 4.66659 7.99992C4.66659 6.15897 6.15897 4.66659 7.99992 4.66659" stroke="#344054" strokeWidth="1.25" strokeLinecap="round" strokeLinejoin="round" />
  89. </g>
  90. <defs>
  91. <clipPath id="clip0_4610_6951">
  92. <rect width="16" height="16" fill="white" />
  93. </clipPath>
  94. </defs>
  95. </svg>
  96. }
  97. const TargetSolidIcon = ({ className }: SVGProps<SVGElement>) => {
  98. return <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" className={className ?? ''}>
  99. <path fillRule="evenodd" clipRule="evenodd" d="M12.7733 0.67512C12.9848 0.709447 13.1669 0.843364 13.2627 1.03504L13.83 2.16961L14.9646 2.73689C15.1563 2.83273 15.2902 3.01486 15.3245 3.22639C15.3588 3.43792 15.2894 3.65305 15.1379 3.80458L13.1379 5.80458C13.0128 5.92961 12.8433 5.99985 12.6665 5.99985H10.9426L8.47124 8.47124C8.21089 8.73159 7.78878 8.73159 7.52843 8.47124C7.26808 8.21089 7.26808 7.78878 7.52843 7.52843L9.9998 5.05707V3.33318C9.9998 3.15637 10.07 2.9868 10.1951 2.86177L12.1951 0.861774C12.3466 0.710244 12.5617 0.640794 12.7733 0.67512Z" fill="#155EEF" />
  100. <path d="M1.99984 7.99984C1.99984 4.68613 4.68613 1.99984 7.99984 1.99984C8.36803 1.99984 8.6665 1.70136 8.6665 1.33317C8.6665 0.964981 8.36803 0.666504 7.99984 0.666504C3.94975 0.666504 0.666504 3.94975 0.666504 7.99984C0.666504 12.0499 3.94975 15.3332 7.99984 15.3332C12.0499 15.3332 15.3332 12.0499 15.3332 7.99984C15.3332 7.63165 15.0347 7.33317 14.6665 7.33317C14.2983 7.33317 13.9998 7.63165 13.9998 7.99984C13.9998 11.3135 11.3135 13.9998 7.99984 13.9998C4.68613 13.9998 1.99984 11.3135 1.99984 7.99984Z" fill="#155EEF" />
  101. <path d="M5.33317 7.99984C5.33317 6.52708 6.52708 5.33317 7.99984 5.33317C8.36803 5.33317 8.6665 5.03469 8.6665 4.6665C8.6665 4.29831 8.36803 3.99984 7.99984 3.99984C5.7907 3.99984 3.99984 5.7907 3.99984 7.99984C3.99984 10.209 5.7907 11.9998 7.99984 11.9998C10.209 11.9998 11.9998 10.209 11.9998 7.99984C11.9998 7.63165 11.7014 7.33317 11.3332 7.33317C10.965 7.33317 10.6665 7.63165 10.6665 7.99984C10.6665 9.4726 9.4726 10.6665 7.99984 10.6665C6.52708 10.6665 5.33317 9.4726 5.33317 7.99984Z" fill="#155EEF" />
  102. </svg>
  103. }
  104. const BookOpenIcon = ({ className }: SVGProps<SVGElement>) => {
  105. return <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg" className={className ?? ''}>
  106. <path opacity="0.12" d="M1 3.1C1 2.53995 1 2.25992 1.10899 2.04601C1.20487 1.85785 1.35785 1.70487 1.54601 1.60899C1.75992 1.5 2.03995 1.5 2.6 1.5H2.8C3.9201 1.5 4.48016 1.5 4.90798 1.71799C5.28431 1.90973 5.59027 2.21569 5.78201 2.59202C6 3.01984 6 3.5799 6 4.7V10.5L5.94997 10.425C5.60265 9.90398 5.42899 9.64349 5.19955 9.45491C4.99643 9.28796 4.76238 9.1627 4.5108 9.0863C4.22663 9 3.91355 9 3.28741 9H2.6C2.03995 9 1.75992 9 1.54601 8.89101C1.35785 8.79513 1.20487 8.64215 1.10899 8.45399C1 8.24008 1 7.96005 1 7.4V3.1Z" fill="#155EEF" />
  107. <path d="M6 10.5L5.94997 10.425C5.60265 9.90398 5.42899 9.64349 5.19955 9.45491C4.99643 9.28796 4.76238 9.1627 4.5108 9.0863C4.22663 9 3.91355 9 3.28741 9H2.6C2.03995 9 1.75992 9 1.54601 8.89101C1.35785 8.79513 1.20487 8.64215 1.10899 8.45399C1 8.24008 1 7.96005 1 7.4V3.1C1 2.53995 1 2.25992 1.10899 2.04601C1.20487 1.85785 1.35785 1.70487 1.54601 1.60899C1.75992 1.5 2.03995 1.5 2.6 1.5H2.8C3.9201 1.5 4.48016 1.5 4.90798 1.71799C5.28431 1.90973 5.59027 2.21569 5.78201 2.59202C6 3.01984 6 3.5799 6 4.7M6 10.5V4.7M6 10.5L6.05003 10.425C6.39735 9.90398 6.57101 9.64349 6.80045 9.45491C7.00357 9.28796 7.23762 9.1627 7.4892 9.0863C7.77337 9 8.08645 9 8.71259 9H9.4C9.96005 9 10.2401 9 10.454 8.89101C10.6422 8.79513 10.7951 8.64215 10.891 8.45399C11 8.24008 11 7.96005 11 7.4V3.1C11 2.53995 11 2.25992 10.891 2.04601C10.7951 1.85785 10.6422 1.70487 10.454 1.60899C10.2401 1.5 9.96005 1.5 9.4 1.5H9.2C8.07989 1.5 7.51984 1.5 7.09202 1.71799C6.71569 1.90973 6.40973 2.21569 6.21799 2.59202C6 3.01984 6 3.5799 6 4.7" stroke="#155EEF" strokeLinecap="round" strokeLinejoin="round" />
  108. </svg>
  109. }
  110. type IExtraInfoProps = {
  111. isMobile: boolean
  112. relatedApps?: RelatedAppResponse
  113. }
  114. const ExtraInfo = ({ isMobile, relatedApps }: IExtraInfoProps) => {
  115. const locale = getLocaleOnClient()
  116. const [isShowTips, { toggle: toggleTips, set: setShowTips }] = useBoolean(!isMobile)
  117. const { t } = useTranslation()
  118. useEffect(() => {
  119. setShowTips(!isMobile)
  120. }, [isMobile, setShowTips])
  121. return <div className='w-full flex flex-col items-center'>
  122. <Divider className='mt-5' />
  123. {(relatedApps?.data && relatedApps?.data?.length > 0) && (
  124. <>
  125. {!isMobile && <div className='w-full px-2 pb-1 pt-4 uppercase text-xs text-gray-500 font-medium'>{relatedApps?.total || '--'} {t('common.datasetMenus.relatedApp')}</div>}
  126. {isMobile && <div className={classNames(s.subTitle, 'flex items-center justify-center !px-0 gap-1')}>
  127. {relatedApps?.total || '--'}
  128. <PaperClipIcon className='h-4 w-4 text-gray-700' />
  129. </div>}
  130. {relatedApps?.data?.map((item, index) => (<LikedItem key={index} isMobile={isMobile} detail={item} />))}
  131. </>
  132. )}
  133. {!relatedApps?.data?.length && (
  134. <FloatPopoverContainer
  135. placement='bottom-start'
  136. open={isShowTips}
  137. toggle={toggleTips}
  138. isMobile={isMobile}
  139. triggerElement={
  140. <div className={classNames('h-7 w-7 inline-flex justify-center items-center rounded-lg bg-transparent', isShowTips && '!bg-gray-50')}>
  141. <QuestionMarkCircleIcon className='h-4 w-4 flex-shrink-0 text-gray-500' />
  142. </div>
  143. }
  144. >
  145. <div className={classNames('mt-5 p-3', isMobile && 'border-[0.5px] border-gray-200 shadow-lg rounded-lg bg-white w-[160px]')}>
  146. <div className='flex items-center justify-start gap-2'>
  147. <div className={s.emptyIconDiv}>
  148. <Squares2X2Icon className='w-3 h-3 text-gray-500' />
  149. </div>
  150. <div className={s.emptyIconDiv}>
  151. <PuzzlePieceIcon className='w-3 h-3 text-gray-500' />
  152. </div>
  153. </div>
  154. <div className='text-xs text-gray-500 mt-2'>{t('common.datasetMenus.emptyTip')}</div>
  155. <a
  156. className='inline-flex items-center text-xs text-primary-600 mt-2 cursor-pointer'
  157. href={
  158. locale === LanguagesSupported[1]
  159. ? 'https://docs.dify.ai/v/zh-hans/guides/knowledge-base/integrate_knowledge_within_application'
  160. : 'https://docs.dify.ai/guides/knowledge-base/integrate-knowledge-within-application'
  161. }
  162. target='_blank' rel='noopener noreferrer'
  163. >
  164. <BookOpenIcon className='mr-1' />
  165. {t('common.datasetMenus.viewDoc')}
  166. </a>
  167. </div>
  168. </FloatPopoverContainer>
  169. )}
  170. </div>
  171. }
  172. const DatasetDetailLayout: FC<IAppDetailLayoutProps> = (props) => {
  173. const {
  174. children,
  175. params: { datasetId },
  176. } = props
  177. const pathname = usePathname()
  178. const hideSideBar = /documents\/create$/.test(pathname)
  179. const { t } = useTranslation()
  180. const { isCurrentWorkspaceDatasetOperator } = useAppContext()
  181. const media = useBreakpoints()
  182. const isMobile = media === MediaType.mobile
  183. const { data: datasetRes, error, mutate: mutateDatasetRes } = useSWR({
  184. url: 'fetchDatasetDetail',
  185. datasetId,
  186. }, apiParams => fetchDatasetDetail(apiParams.datasetId))
  187. const { data: relatedApps } = useSWR({
  188. action: 'fetchDatasetRelatedApps',
  189. datasetId,
  190. }, apiParams => fetchDatasetRelatedApps(apiParams.datasetId))
  191. const navigation = useMemo(() => {
  192. const baseNavigation = [
  193. { name: t('common.datasetMenus.hitTesting'), href: `/datasets/${datasetId}/hitTesting`, icon: TargetIcon, selectedIcon: TargetSolidIcon },
  194. // { name: 'api & webhook', href: `/datasets/${datasetId}/api`, icon: CommandLineIcon, selectedIcon: CommandLineSolidIcon },
  195. { name: t('common.datasetMenus.settings'), href: `/datasets/${datasetId}/settings`, icon: Cog8ToothIcon, selectedIcon: Cog8ToothSolidIcon },
  196. ]
  197. if (datasetRes?.provider !== 'external') {
  198. baseNavigation.unshift({
  199. name: t('common.datasetMenus.documents'),
  200. href: `/datasets/${datasetId}/documents`,
  201. icon: DocumentTextIcon,
  202. selectedIcon: DocumentTextSolidIcon,
  203. })
  204. }
  205. return baseNavigation
  206. }, [datasetRes?.provider, datasetId, t])
  207. useEffect(() => {
  208. if (datasetRes)
  209. document.title = `${datasetRes.name || 'Dataset'} - 智脑云平台`
  210. }, [datasetRes])
  211. const setAppSiderbarExpand = useStore(state => state.setAppSiderbarExpand)
  212. useEffect(() => {
  213. const localeMode = localStorage.getItem('app-detail-collapse-or-expand') || 'expand'
  214. const mode = isMobile ? 'collapse' : 'expand'
  215. setAppSiderbarExpand(isMobile ? mode : localeMode)
  216. }, [isMobile, setAppSiderbarExpand])
  217. if (!datasetRes && !error)
  218. return <Loading />
  219. return (
  220. <div className='grow flex overflow-hidden'>
  221. {!hideSideBar && <AppSideBar
  222. title={datasetRes?.name || '--'}
  223. icon={datasetRes?.icon || 'https://static.dify.ai/images/dataset-default-icon.png'}
  224. icon_background={datasetRes?.icon_background || '#F5F5F5'}
  225. desc={datasetRes?.description || '--'}
  226. isExternal={datasetRes?.provider === 'external'}
  227. navigation={navigation}
  228. extraInfo={!isCurrentWorkspaceDatasetOperator ? mode => <ExtraInfo isMobile={mode === 'collapse'} relatedApps={relatedApps} /> : undefined}
  229. iconType={datasetRes?.data_source_type === DataSourceType.NOTION ? 'notion' : 'dataset'}
  230. />}
  231. <DatasetDetailContext.Provider value={{
  232. indexingTechnique: datasetRes?.indexing_technique,
  233. dataset: datasetRes,
  234. mutateDatasetRes: () => mutateDatasetRes(),
  235. }}>
  236. <div className="bg-white grow overflow-hidden">{children}</div>
  237. </DatasetDetailContext.Provider>
  238. </div>
  239. )
  240. }
  241. export default React.memo(DatasetDetailLayout)